<template>
  <div class="edit">
    <el-form :model="formData" label-width="100px" ref="form" style="width: 600px" size="small">
      <el-form-item label="商品名称">
        <el-input v-model="formData.product_name"></el-input>
      </el-form-item>
      <el-form-item label="商品分类">
        <el-select v-model="formData.category_id" placeholder="请选择商品分类">
          <el-option
            :label="item.category_name"
            :value="item.category_id"
            v-for="(item,index) in categoryList"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品标题">
        <el-input v-model="formData.product_title"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.product_intro"></el-input>
      </el-form-item>
      <el-form-item label="图片">
        <el-upload
          action
          class="upload-demo"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          list-type="picture"
          name="file"
          :http-request="action"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="价格">
        <el-input type="number" v-model="formData.product_price"></el-input>
      </el-form-item>
      <el-form-item label="折扣价">
        <el-input type="number" v-model="formData.product_selling_price"></el-input>
      </el-form-item>
      <el-form-item label="数量">
        <el-input type="number" v-model="formData.product_num"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { detailProduct, addEditProduct } from "@/api/shop";
import { classifyList } from "@/api/category";
import { uploadFile } from "@/api/police";
export default {
  data() {
    return {
      formData: {
        product_name: "",
        category_id: "",
        product_title: "",
        product_intro: "",
        product_picture: "",
        product_price: "",
        product_selling_price: "",
        product_num: ""
      },
      categoryList: [],
      fileList: [
        // { url: "http://127.0.0.1:3006/public/imgs/phone/Redmi-k30.png" }
      ]
    };
  },
  mounted() {
    classifyList().then(res => {
      this.categoryList = res.data;
    });
    let { id } = this.$route.query;
    if (id) {
      detailProduct({ id }).then(res => {
        this.formData = res.data;
      });
    }
  },
  methods: {
    handleRemove(file, fileList) {
      if (fileList.length == 0) {
        this.formData.product_picture = "";
      }
    },
    handlePreview(file) {
      console.log(file);
    },
    action(data) {
      let form = new FormData();
      form.append("file", data.file);
      uploadFile(form).then(res => {
        this.formData.product_picture = res.data.url;
      });
    },
    submitForm() {
      addEditProduct(this.formData).then(res => {
        if (res.code == 200) {
          this.$message.success(res.message);
          setTimeout(() => {
            this.$router.push("/cms/list");
          }, 500);
        }
      });
    },
    resetForm() {
      this.$refs["form"].resetFields();
      this.formData = {};
    }
  }
};
</script>

<style lang="scss" scoped>
.edit {
  width: 100%;
  margin: 10px auto;
}
</style>